package xyf.p07.binsearch;

/**
 * ClassName: P0704_BinSearch
 * Description:
 * Author: xyf
 * Date: 9/7/22 3:37 PM
 * Version: 1.0
 **/
public class P0704_BinSearch
{
    public int search(int[] nums, int target)
    {
        if (nums == null || nums.length < 1)
        {
            return -1;
        }
        int start = 0;
        int end = nums.length - 1;
        while (start <= end)
        {
            int middle = (end - start) / 2 + start;
            if (nums[middle] == target)
            {
                return middle;
            }
            else if (nums[middle] < target)
            {
                start = middle + 1;
            }
            else
            {
                end = middle - 1;
            }

        }
        return -1;
    }
}
